
import TTSDK from "./sdk/TTSDK";
import OppoSDK from "./sdk/OppoSDK";
import VivoSDK from "./sdk/VivoSDK";
import QQSDK from "./sdk/QQSDK";
import SwanSDK from "./sdk/SwanSDK";
import NativeSDK from "./sdk/NativeSDK";
import CocosSDK from "./sdk/CocosSDK";
import F399SDK from "./sdk/F399SDK";
import SDKAdatper from "./core/SDKAdatper";
import WXSDK from "./sdk/WXSDK";
import KSSDK from "./sdk/KSSDK";
import { PlatName, QG, SDK_NAMES } from "./AdapterDefine";
import { DevSDK } from "./sdk/DevSDK";
import { JsNativeBridge } from "./native/JsNativeBridge";
import { Native } from "./native/native";


/**
 * SDK工厂：纯屏蔽sdk接口使用。
 */
export default class SDKFactory {
    private static _sdk: SDKAdatper = null;
    static get sdk() {
        if (!this._sdk) {
            this.init();
        }
        return this._sdk;
    }
    static init() {
        let sdk: SDKAdatper = this._sdk;
        if (!sdk) {
            let c = this.getSDK();
            let platform = this.getPlatName();
            switch (platform) {
                case PlatName.WX:
                    sdk = new WXSDK(c, platform)
                    break;
                case PlatName.TT:
                    sdk = new TTSDK(c, platform)
                    break;
                case PlatName.OPPO:
                    sdk = new OppoSDK(c, platform)
                    break;
                case PlatName.VIVO:
                    sdk = new VivoSDK(c, platform)
                    break;
                case PlatName.QQ:
                    sdk = new QQSDK(c, platform)
                    break;
                case PlatName.SWAN:
                    sdk = new SwanSDK(c, platform)
                    break;
                case PlatName.NATIVE:
                    sdk = new NativeSDK(c, platform)
                    break;
                case PlatName.COCOS:
                    sdk = new CocosSDK(c, platform)
                    break;
                case PlatName.F399:
                    sdk = new F399SDK(c, platform)
                    break;
                case PlatName.KS:
                    sdk = new KSSDK(c, platform)
                    break;
                case PlatName.DEV:
                    sdk = new DevSDK(c, platform)
                    break;

            }
            this._sdk = sdk;
        }
        window['sdk'] = this._sdk;
        return sdk
    }

    private static getSDK() {
        for (let index = 0; index < SDK_NAMES.length; index++) {
            const element: any = SDK_NAMES[index];
            if (window[element]) {
                return window[element]
            }
        }
        if (JsNativeBridge.isNative()) {
            window['JsNativeBridge'] = JsNativeBridge;
            return Native;
        }
        return null;
    }

    private static getPlatName() {
        for (let index = 0; index < SDK_NAMES.length; index++) {
            let platfom: any = SDK_NAMES[index];
            const sdk: any = window[platfom];
            if (sdk) {
                let qg: any = QG
                if (platfom == qg) {
                    let sys = sdk.getSystemInfoSync()
                    console.log(' sys ', sys.brand)
                    return sys.brand.toLowerCase();
                }
            }
        }
        if (JsNativeBridge.isNative()) {
            return PlatName.NATIVE
        } else {
            return PlatName.DEV;
        }
    }

}